// pages/login/login.js
import { request } from '../../../../../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: true,   //控制注册按钮的显示
    firstName:"",     //保存一系列的注册数据
    username: "",
    password: "",
    passwordTwo:"",
    org:"",
    // current: 'tab1',

    // username1:"",
    // password1:"",
    // disabled1:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      const org = options.org
      this.setData({
        ...this.data,
        org:org
      })
  },
  /**
   * 控制tab切换
   * @param {*} e 
   */
  handleChange ({ detail }) {
    this.setData({
        current: detail.key
    });
},
  /**
   * 注册
   * sessionid
   */
  formSubmit(e) {
    const that = this;
   
    if(this.data.password !== this.data.passwordTwo){
      wx.showToast({
        title: '两次输入的密码不相同，请重新输入',
        icon:"none"
      })
      this.setData({
        ...this.data,
        password:"",
        passwordTwo:"",
        disabled:true
      })
      return
    }
    const param = {
      org:this.data.org,
      first_name:this.data.firstName,
      password:this.data.password,
      phone:this.data.username
    }

    request("POST", "/api/auth/register/", param, "", true,"注册中")
    .then(res =>{
      if(res.data.code === "A0111"){
        wx.showToast({
          title: '该手机号已存在',
          icon:"none"
        })
        that.setData({
          ...this.data,
          username:"",
          password:"",
          passwordTwo:"",
        })
      }else if(res.data.msg === "注册成功"){
        const csrf = res.cookies[0] || ""
        const sessionId = res.cookies[1] || ""
        const a = csrf.split(";")[0]
        const b = a.split("=")[1]
        const b_name = a.split("=")[0]
        const c = sessionId.split(";")[0]
        const d = c.split("=")[1]
        const d_name = c.split("=")[0]
        if (b_name === "csrftoken") {
          wx.setStorageSync('csrf', b)
        } else {
          wx.setStorageSync('csrf', d)
        }
        wx.setStorageSync(b_name, b_name + "=" + b)
        wx.setStorageSync(d_name, d_name + "=" + d)
        wx.setStorageSync('userInfo', res.data.userInfo)
        
        wx.switchTab({
          url: '/pages/tabBar/search/search',
        })
      }else{
        wx.showToast({
          title: '注册失败，服务器错误',
          icon:"none"
        })
      }
    })
    //   .then(
    //     (res) => {
    //       if (res.statusCode === 200) {
    //         const csrf = res.cookies[0]
    //         const a = csrf.split(";")[0]
    //         const b = a.split("=")[1]
    //         console.log(b)
    //         console.log(res)
    //         console.log(res.data.token)
    //         wx.setStorageSync('csrf',b)
    //         wx.setStorageSync('csrftoken', "csrftoken=" + b)
    //         wx.setStorageSync('sessionid',"sessionid=" +  res.data.token)
    //         wx.setStorageSync('userInfo', res.data.userInfo)
    //         console.log("验证成功")
    //         wx.switchTab({
    //           url: '../tabBar/search/search',
    //         })
    //       } else if (res.statusCode === 404) {
    //         console.log("验证失败");
    //         wx.showToast({
    //           title: '服务器出错请稍后再试',
    //           icon: 'none',
    //           duration: 2000
    //         })
    //       } else if(res.statusCode === 500){
    //         wx.showToast({
    //           title: '服务器出错请稍后再试',
    //           icon: 'none',
    //           duration: 2000
    //         })
    //       } else {
    //         wx.showToast({
    //           title: '账号或密码错误',
    //           icon: 'none',
    //           duration: 2000
    //         })
    //         that.setData({
    //           password: '',
    //           disabled:true
    //         })

    //       }
    //     }

    //   )
  },


  /**
   * 检测按钮是否可用
   */
  setnameVal(e){
    
    this.setData({
      firstName: e.detail.value
    })
    if (this.data.username.length >= 1 && this.data.password.length >= 1 && this.data.passwordTwo.length >= 1 &&this.data.firstName.length >= 1) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  setuserVal(e) {
    
    this.setData({
      username: e.detail.value
    })
    if (this.data.username.length >= 1 && this.data.password.length >= 1 && this.data.passwordTwo.length >= 1 &&this.data.firstName.length >= 1) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  setpassVal(e) {
    this.setData({
      password: e.detail.value
    })
    if (this.data.username.length >= 1 && this.data.password.length >= 1 && this.data.passwordTwo.length >= 1 &&this.data.firstName.length >= 1) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  setpassVal2(e){
    this.setData({
      passwordTwo:e.detail.value
    })
    if (this.data.username.length >= 1 && this.data.password.length >= 1 && this.data.passwordTwo.length >= 1 &&this.data.firstName.length >= 1) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
    /**
   * 两个协议
   */
  consumer(e) {
    
    wx.navigateTo({
      url: '/pages/webView/consumer/consumer',
      success: res => {

      }
    })
  },
  privacy(e) {
    
    wx.navigateTo({
      url: '/pages/webView/privacy/privacy',
      success: res => {

      }
    })
  },

  // /**
  //  *登录并加入 
  //  */
  // formSubmit1(){

  // },

  // setuserVal1(e) {
    
  //   this.setData({
  //     username1: e.detail.value
  //   })
  //   if (this.data.username1.length >= 1 && this.data.password1.length >= 1) {
  //     this.setData({
  //       disabled1: false
  //     })
  //   } else {
  //     this.setData({
  //       disabled1: true
  //     })
  //   }
  // },
  // setpassVal1(e) {
  //   this.setData({
  //     password1: e.detail.value
  //   })
  //   if (this.data.username1.length >= 1 && this.data.password1.length >= 1) {
  //     this.setData({
  //       disabled1: false
  //     })
  //   } else {
  //     this.setData({
  //       disabled1: true
  //     })
  //   }
  // },
})